package com.bawei.admin.test;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import java.util.HashMap;

public class Sample {


    // 设置APPID/AK/SK
    public static final String APP_ID = "25331473";
    public static final String API_KEY = "YG3QaDAnF7zr47UlT5PGG80b";
    public static final String SECRET_KEY = "atA51z9TekTRUotYv2PXdt3pjvLwQ2Th";



    // 初始化用户对象
    public static AipOcr init() {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        return client;
    }

    public void sample() {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");

        AipOcr client = init();

        // 参数为本地图片路径
//        String image = "test.jpg";
//        String path = "E:\\图片\\me\\2021-12-12_120341.jpg";
//        JSONObject res = client.basicGeneral(path, options);
//        System.out.println(res.toString());

//        // 参数为本地图片二进制数组
//        byte[] file = readImageFile(image);
//        res = client.basicGeneral(file, options);
//        System.out.println(res.toString(2));


        // 通用文字识别, 图片参数为远程url图片
        JSONObject res = client.basicGeneralUrl("https://liuke-images.oss-cn-shanghai.aliyuncs.com/2021-12-12_111858.png", options);
        System.out.println(res.toString(2));
    }

    public static void main(String[] args) {
        new Sample().sample();
    }

}
